package chenji.yourticket;

import java.io.File;
import java.util.logging.Logger;

import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import chenji.yourticket.command.CommandYT;
import chenji.yourticket.configuration.ConfigException;
import chenji.yourticket.configuration.configurations.Config;
import chenji.yourticket.configuration.configurations.MessageConfig;
import chenji.yourticket.datacenter.PlayerDataCenter;
import chenji.yourticket.editor.EditorManager;
import chenji.yourticket.editor.MaterialManager;
import chenji.yourticket.inventory.InventoryManager;
import chenji.yourticket.model.ModelManager;
import chenji.yourticket.press.PressManager;
import chenji.yourticket.pricecalculator.LimitManager;
import chenji.yourticket.pricecalculator.VIPDiscountManager;
import net.milkbowl.vault.economy.Economy;

public class Main extends JavaPlugin {
	private static Logger log;
	private static File Folder;
	private static MessageConfig message;
	private static ModelManager mm;
	private static MaterialManager materialmanager;
	private static Config config;
	private static VIPDiscountManager vdm;
	private static LimitManager lm;
	private static PlayerDataCenter pdc;
	private static EditorManager em;
	private static InventoryManager im;
	private static Economy economy = null;
	private boolean loadDone;
	private static PressManager pm;
	@Override
	public void onEnable() {
		getLogger().info("--------------YourTicket插件启动中------------");
		getLogger().info("作者：ChenJi");
		
		if (!this.getDataFolder().exists()) {
			this.getDataFolder().mkdirs();
		}
		if (!(new File(this.getDataFolder() + "/PlayerData/").exists())) {
			new File(this.getDataFolder() + "/PlayerData/").mkdirs();
		}
		Main.log = getLogger();
		Main.Folder = getDataFolder();
		try {
			message = new MessageConfig();
		} catch (ConfigException e) {
			e.printStackTrace();
		}
		setupEconomy();
		pdc = new PlayerDataCenter();
		materialmanager = new MaterialManager();
		mm = new ModelManager();
		vdm = new VIPDiscountManager();
		lm = new LimitManager();
		em = new EditorManager();
		im = new InventoryManager();
		pm = new PressManager();
		try {
			config = new Config();
		} catch (ConfigException e) {
			e.printStackTrace();
		}
		getCommand("YT").setExecutor(new CommandYT());
		getServer().getPluginManager().registerEvents(new YListener(), this);
		this.loadDone=true;
		getLogger().info("--------------------------------------");

	}
	@Override
	public void onDisable() {
		if (this.loadDone) {
			Main.getEditorManager().unload();
			Main.getPressManager().unload();
			Main.getModelManager().unload();
		}
		getLogger().info("YourTicket插件卸载完成");
	}

	public void setupEconomy() {
		RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager()
				.getRegistration(net.milkbowl.vault.economy.Economy.class);
		if (economyProvider != null) {
			economy = economyProvider.getProvider();
		}
	}

	public static Economy getEconomy() {
		return Main.economy;
	}

	public static VIPDiscountManager getVIPDiscountManager() {
		return Main.vdm;
	}

	public static PressManager getPressManager() {
		return Main.pm;
	}

	public static Config getConfiguration() {
		return Main.config;
	}

	public static MaterialManager getMaterialManager() {
		return Main.materialmanager;
	}

	public static Logger getLog() {
		return Main.log;
	}

	public static File getKDataFolder() {
		return Main.Folder;
	}

	public static MessageConfig getMessageManager() {
		return Main.message;
	}

	public static ModelManager getModelManager() {
		return Main.mm;
	}

	public static LimitManager getLimitManager() {
		return Main.lm;
	}

	public static PlayerDataCenter getPlayerDataChenter() {
		return Main.pdc;
	}

	public static EditorManager getEditorManager() {
		return Main.em;
	}

	public static InventoryManager getInventoryManager() {
		return Main.im;
	}

}
